package xyz.yuzegod.megawalls.classes.normal.squid;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class SecondSkill extends Skill
{
    public SecondSkill(final Classes classes) {
        super("喷墨", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 2.0;
            }
            case 2: {
                return 3.0;
            }
            case 3: {
                return 4.0;
            }
            default: {
                return 2.0;
            }
        }
    }
    
    public double getSeconds(final int level) {
        switch (level) {
            case 1: {
                return 0.8;
            }
            case 2: {
                return 1.6;
            }
            case 3: {
                return 2.4;
            }
            default: {
                return 0.8;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7当你喝完药水的时候,");
            lore.add("   §7令§a" + this.getAttribute(level) + "§7格范围内的敌人失明§a" + this.getSeconds(level) + "§7秒。");
            return lore;
        }
        lore.add(" §8▪ §7当你喝完药水的时候,");
        lore.add("   §7令§8" + this.getAttribute(level - 1) + " ➜");
        lore.add("   §a" + this.getAttribute(level) + "§7格范围内的敌人失明§8" + this.getSeconds(level - 1) + " ➜ §a" + this.getSeconds(level) + "§7秒。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill2Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill2Level();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final int level = kitStats.getSkill2Level();
        for (final Player player : this.getNearbyPlayers(gamePlayer.getPlayer(), this.getAttribute(level))) {
            if (player.hasPotionEffect(PotionEffectType.BLINDNESS)) {
                player.removePotionEffect(PotionEffectType.BLINDNESS);
            }
            player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, (int)(this.getSeconds(level) * 20.0), 0));
        }
        return true;
    }
    
    private List<Player> getNearbyPlayers(final Player player, final double radius) {
        final List<Player> players = new ArrayList<Player>();
        for (final Player other : PlayerUtils.getNearbyPlayers((Entity)player, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator()) {
                if (GamePlayer.get(player.getUniqueId()).getGameTeam().isInTeam(gameOther)) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
}
